<?php
if(isset($_COOKIE['alreadyChoiceCourse'])){
	header('location:../staticPage/alreadyChoiceCourse.html');
}
?>
<!DOCTYPE html>
<html>
<head>
    <title>今日选课</title>
    <meta charset="utf-8">
    <meta http-equiv="X-UA-Compatible" content="IE=edge">
    <meta name="viewport" content="width=device-width, initial-scale=1, user-scalable=no">

    <meta name="description" content="Write an awesome description for your new site here. You can edit this line in _config.yml. It will appear in your document head meta (for Google search results) and in your feed.xml site description.
">
     
    <link rel="stylesheet" href="../css/weui.css" type="text/css">
    <link rel="stylesheet" href="https://cdn.bootcss.com/weui/0.4.3/style/weui.min.css" type="text/css">
    <link rel="stylesheet" href="https://cdn.bootcss.com/jquery-weui/1.2.0/css/jquery-weui.min.css">
    <link rel="stylesheet" href="../css/demos.css">
    <script type="text/javascript" src="../js/layui/layui.js"></script>
</head>
  <body ontouchstart>
    <header class='demos-header' style="padding-top: 10px;padding-bottom: 5px;">
      <h1 class="demos-title" ><?php echo $_GET['roomID']?></h1>
    </header>

    <div class="weui-cells weui-cells_form">
      <div class="weui-cell">
        <div class="weui-cell__hd"><label for="name" class="weui-label">课程名</label></div>
        <div class="weui-cell__bd">
          <input class="weui-input" id="courses" type="text" value="">
        </div>
      </div>

      <div class="weui-cell">
        <div class="weui-cell__hd"><label for="name" class="weui-label">时段</label></div>
        <div class="weui-cell__bd">
          <input class="weui-input" id="coursetime" type="text" value="">
        </div>
      </div>
    </div>
	
	<div class='demos-content-padded'>
        <div class="button_sp_area">
            <button class="weui-btn weui-btn_mini weui-btn_primary" onclick="save()">确定</<button>
        </div>
    </div>
	
	<script src="../js/jquery-2.1.4.js"></script>
	<script src="../js/fastclick.js"></script>
	<script>   
	 $(function() {     
	          FastClick.attach(document.body); 
			  }); 
	</script> 
	<script src="https://cdn.bootcss.com/jquery-weui/0.8.0/js/jquery-weui.min.js"></script>
    <script>
	        function GetCourseName(ctrlName, dictTypeName, onChange, onClose, onOpen) {
                var url = './<?php echo $_GET['roomID']?>.json';

                //获取Ajax的内容，并放到items集合
                var control = $('#' + ctrlName);
                var items = [];
                $.ajax({
                    type: 'GET',
                    url: url,
                    //async: false, //同步
                    dataType: 'json',
                    success: function (data) {
						
                        control.empty();//清空下拉框
                        //把JSON集合加到数组里面
						
                        $.each(data, function (i, item) {
                            items.push({
                                title: item[1], value: item[0]
                            });
                        });
                         
                        //设置显示列表
                        control.select({
                            title: "选择" + dictTypeName,
                            items: items,
							multi: true,
							min: 0,
                            max: 4,
                            onChange: onChange,
                            onClose: onClose,
                            onOpen: onOpen
                        });
                    },
                    error: function (xhr) {
                        alert("操作失败" + xhr.responseText +xhr.status + xhr.statusText ); //xhr.responseText
					}
                });
            }
			
			
			 function GetTimeItem(ctrlName, dictTypeName, onChange, onClose, onOpen) {
                var url = './time.json';

                //获取Ajax的内容，并放到items集合
                var control = $('#' + ctrlName);
                var items = [];
                $.ajax({
                    type: 'GET',
                    url: url,
                    //async: false, //同步
                    dataType: 'json',
                    success: function (data) {
					
                        control.empty();//清空下拉框
                        //把JSON集合加到数组里面
						var now = new Date();
                        var hours= now.getHours();
                        var minutes= now.getMinutes();
                        var hoursNumber=Number(hours);
                        var minutesNumber=Number(minutes);
                        var timeNumber=""+hoursNumber+minutesNumber;
                        var timeNowNumber=Number(timeNumber);
						if((timeNowNumber>=600)&&(timeNowNumber<745)){	  
						  items = [
								  {
									title: "上午1~2节(08:00-09:35)",
									value: 1,
								  },
								  {
									title: "上午3~4节(10:00-11:35)",
									value: 2,
								  },
								  {
									title: "下午5~6节(13:30-15:05)",
									value: 3,
								  },
								  {
									title: "下午7~8节(15:15-16:50)",
									value: 4,
								  },
								  {
									title: "晚上9~10节(18:00-20:30)",
									value: 5,
								  }
							    ];
                        }
						if((timeNowNumber>=745)&&(timeNowNumber<945)){
	                        items =[
									  {
										title: "上午3~4节(10:00-11:35)",
										value: 2,
									  },
									  {
										title: "下午5~6节(13:30-15:05)",
										value: 3,
									  },
									  {
										title: "下午7~8节(15:15-16:50)",
										value: 4,
									  },
									  {
										title: "晚上9~10节(18:00-20:30)",
										value: 5,
									  }
								 ];
                        }
						
                        if((timeNowNumber>=945)&&(timeNowNumber<1315)){
		                    items =[
									  {
										title: "下午5~6节(13:30-15:05)",
										value: 3,
									  },
									  {
										title: "下午7~8节(15:15-16:50)",
										value: 4,
									  },
									  {
										title: "晚上9~10节(18:00-20:30)",
										value: 5,
									  }
								 ];
						}
                         if((timeNowNumber>=1315)&&(timeNowNumber<1500)){
							items =[
									  {
										title: "下午7~8节(15:15-16:50)",
										value: 4,
									  },
									  {
										title: "晚上9~10节(18:00-20:30)",
										value: 5,
									  }
								 ];
					    }
						
						if((timeNowNumber>=1500)&&(timeNowNumber<1745)){      
							items =[
									  {
										title: "晚上9~10节(18:00-20:30)",
										value: 5,
									  }
								 ];  
						}
						if((timeNowNumber>=1745)&&(timeNowNumber<2345)){
                               items = [
								  {
									title: "上午1~2节(08:00-09:35)",
									value: 1,
								  },
								  {
									title: "上午3~4节(10:00-11:35)",
									value: 2,
								  },
								  {
									title: "下午5~6节(13:30-15:05)",
									value: 3,
								  },
								  {
									title: "下午7~8节(15:15-16:50)",
									value: 4,
								  },
								  {
									title: "晚上9~10节(18:00-20:30)",
									value: 5,
								  }
							    ];
                        }
						if(timeNowNumber>=2345){
							items = [];
						}
						
						
						
                        //设置显示列表
                        control.select({
                            title: "选择" + dictTypeName,
                            items: items,
							multi: true,
							min: 0,
                            max: 4,
                            onChange: onChange,
                            onClose: onClose,
                            onOpen: onOpen
                        });
                    },
                    error: function (xhr) {
                        alert("操作失败" + xhr.responseText +xhr.status + xhr.statusText ); //xhr.responseText
                        
					}
                });
            }
			
			$(function () {
                GetCourseName("courses", "课程名");
                GetTimeItem("coursetime", "时段");
            });
			 
		    layui.use(['layer'],function(){
		     layer=layui.layer;
		     $=layui.jquery;
		   });
		
            function save(){
				var courses = $.trim($('#courses').val());
				var coursetime = $.trim($('#coursetime').val());
				
				if(courses == ''){
					layer.alert('请选择课程名',{icon:2});
					return;
				}
				if(coursetime == ''){
					layer.alert('请选择时段',{icon:2});
					return;
				}
				 $.post('./courseSelectionRecord.php',{'roomID':<?php echo $_GET['roomID']?>,'courses':courses,'coursetime':coursetime},function(res){
					if(res.code == 0){
						layer.alert(res.msg,{icon:2});
					}else{
						layer.msg(res.msg);
						setTimeout(function(){window.location.href ="../alreadyChoiceCourse.php";},1000);
					}
				},'json');
		    }
    </script>
  </body>
</html>
